﻿<?xml version="1.0" encoding="UTF-8" ?>
<!-- 
    This file is part of Microsoft Robotics Developer Studio Code Samples.
    Copyright (C) Microsoft Corporation.  All rights reserved.
    $File: IRSeekerV2.xslt $ $Revision: 1 $
-->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:a="http://schemas.microsoft.com/robotics/2009/12/hitechnic/nxt/irseekerv2.html"
                xmlns:c="http://schemas.microsoft.com/robotics/2007/07/lego/nxt/common.html">
  <xsl:import href="/resources/dss/Microsoft.Dss.Runtime.Home.MasterPage.xslt" />

  <xsl:template match="/">
    <xsl:call-template name="MasterPage">
      <xsl:with-param name="title">
        <xsl:text>HiTechnic IR Seeker V2 - </xsl:text>
        <xsl:value-of select="/a:IRSeekerV2State/a:Name"/>
      </xsl:with-param>
      <xsl:with-param name="serviceName">
        HiTechnic IR Seeker V2
      </xsl:with-param>
      <xsl:with-param name="description">
        Status of a HiTechnic IR Seeker V2.
      </xsl:with-param>
      <xsl:with-param name="head">
        <script language="javascript" type="text/javascript" src="/resources/HiTechnicSensors.Y2009.M12/Microsoft.Robotics.Services.Sample.HiTechnic.Transforms.Common.js"/>
        <script language="javascript" type="text/javascript">
          <xsl:text>var pollingFrequency = </xsl:text><xsl:value-of select="/a:IRSeekerV2State/a:PollingFrequencyMs"/>
          <xsl:text>;</xsl:text>
          <xsl:text>var timeStamp = "</xsl:text><xsl:value-of select="/a:IRSeekerV2State/a:Reading/a:TimeStamp"/>
          <xsl:text>";</xsl:text>
          <xsl:text>var Mode = "</xsl:text><xsl:value-of select="/a:IRSeekerV2State/a:Mode"/>
          <xsl:text>";</xsl:text>
          <xsl:text>var DCDirection = </xsl:text><xsl:value-of select="/a:IRSeekerV2State/a:Reading/a:DCDirection"/>
          <xsl:text>;</xsl:text>
          <xsl:text>var DCMean = </xsl:text><xsl:value-of select="/a:IRSeekerV2State/a:Reading/a:DCMean"/>
          <xsl:text>;</xsl:text>
          <xsl:text>var ACDirection = </xsl:text><xsl:value-of select="/a:IRSeekerV2State/a:Reading/a:ACDirection"/>
          <xsl:text>;</xsl:text>

          <![CDATA[

var firstTime = true;

function onState(state)
{
    if (state.Reading.TimeStamp != timeStamp || firstTime)
    {
      firstTime = false;
      timeStamp = state.Reading.TimeStamp;
      //document.all("TimeStamp").innerText = timeStamp;
      
      Mode = state.Mode;
      document.all("Mode").innerText = Mode;
      document.all("PollingFrequencyMs").innerText = state.PollingFrequencyMs;

      for (var key in state.Reading)
      {
        var cell = document.all(key);
        if (cell != null)
        {
          cell.innerText = state.Reading[key];
        }
      }
      ACDirection = state.Reading.ACDirection;
      DCDirection = state.Reading.DCDirection;
      showDiag();
    }

    readState(pollingFrequency);
}

function showDiag()
{
  var diagram = document.all("diagram");
  var val;
  if (Mode == "Modulated")
    val = ACDirection;
  else
    val = DCDirection;
  val = parseInt(val);
  switch (val)
  {
    case 1:
      diagram.src = "/resources/HiTechnicSensors.Y2009.M12/Microsoft.Robotics.Services.Sample.HiTechnic.Resources.IRSeeker_Diag1.png";
      break;
    case 2:
      diagram.src = "/resources/HiTechnicSensors.Y2009.M12/Microsoft.Robotics.Services.Sample.HiTechnic.Resources.IRSeeker_Diag2.png";
      break;
    case 3:
      diagram.src = "/resources/HiTechnicSensors.Y2009.M12/Microsoft.Robotics.Services.Sample.HiTechnic.Resources.IRSeeker_Diag3.png";
      break;
    case 4:
      diagram.src = "/resources/HiTechnicSensors.Y2009.M12/Microsoft.Robotics.Services.Sample.HiTechnic.Resources.IRSeeker_Diag4.png";
      break;
    case 5:
      diagram.src = "/resources/HiTechnicSensors.Y2009.M12/Microsoft.Robotics.Services.Sample.HiTechnic.Resources.IRSeeker_Diag5.png";
      break;
    case 6:
      diagram.src = "/resources/HiTechnicSensors.Y2009.M12/Microsoft.Robotics.Services.Sample.HiTechnic.Resources.IRSeeker_Diag6.png";
      break;
    case 7:
      diagram.src = "/resources/HiTechnicSensors.Y2009.M12/Microsoft.Robotics.Services.Sample.HiTechnic.Resources.IRSeeker_Diag7.png";
      break;
    case 8:
      diagram.src = "/resources/HiTechnicSensors.Y2009.M12/Microsoft.Robotics.Services.Sample.HiTechnic.Resources.IRSeeker_Diag8.png";
      break;
    case 9:
      diagram.src = "/resources/HiTechnicSensors.Y2009.M12/Microsoft.Robotics.Services.Sample.HiTechnic.Resources.IRSeeker_Diag9.png";
      break;
    default:
      diagram.src = "/resources/HiTechnicSensors.Y2009.M12/Microsoft.Robotics.Services.Sample.HiTechnic.Resources.IRSeeker_Diag.png";
      break;
  }
}

function startPolling()
{
  setStateCompletion("onState");
  readState(pollingFrequency);
}

setTimeout("startPolling()", 1000);

          ]]>
        </script>
      </xsl:with-param>
    </xsl:call-template>
  </xsl:template>

  <!--<xsl:output method="html"/>-->

  <xsl:template match="/a:IRSeekerV2State">
    <img src="/resources/HiTechnicSensors.Y2009.M12/Microsoft.Robotics.Services.Sample.HiTechnic.Resources.HiTechnicIRSeekerV2.Image.png"
         align="right" alt="Buttons" width="32px" height="32px"/>
    <table>
      <tr class="even">
        <th width="30%">Name:</th>
        <td width="70%">
          <xsl:value-of select="a:Name"/>
        </td>
      </tr>
      <tr class="odd">
        <th>Connection:</th>
        <td>
          <xsl:choose>
            <xsl:when test="a:Connected = 'true'">
              Connected
            </xsl:when>
            <xsl:otherwise>
              Not connected
            </xsl:otherwise>
          </xsl:choose>
          <xsl:text> on </xsl:text>
          <xsl:value-of select="a:SensorPort"/>
        </td>
      </tr>
      <tr class="even">
        <th>Polling Frequency:</th>
        <td id="PollingFrequencyMs">
          <xsl:value-of select="a:PollingFrequencyMs"/>
        </td>
      </tr>
      <tr class="odd">
        <th>Mode:</th>
        <td id="Mode">
          <xsl:value-of select="a:Mode"/>
        </td>
      </tr>
      <tr class="even">
        <th>DC Direction:</th>
        <td id="DCDirection">
          <xsl:value-of select="a:Reading/a:DCDirection"/>
        </td>
      </tr>
      <tr class="odd">
        <th>DC Mean:</th>
        <td id="DCMean">
          <xsl:value-of select="a:Reading/a:DCMean"/>
        </td>
      </tr>
      <tr class="even">
        <th>AC Direction:</th>
        <td id="ACDirection">
          <xsl:value-of select="a:Reading/a:ACDirection"/>
        </td>
      </tr>
      <tr class="odd">
        <th>Timestamp:</th>
        <td id="TimeStamp">
          <xsl:value-of select="a:Reading/a:TimeStamp"/>
        </td>
      </tr>
    </table>
    <img id="diagram" src="/resources/HiTechnicSensors.Y2009.M12/Microsoft.Robotics.Services.Sample.HiTechnic.Resources.IRSeeker_Diag.png" />

  </xsl:template>
</xsl:stylesheet>